package ar.edu.utn.frsf.tpae.a2013.g04.presentation.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import ar.edu.utn.frsf.tpae.a2013.g04.persistence.model.SolicitudGasto;
import ar.edu.utn.frsf.tpae.a2013.g04.persistence.model.Supervisor;
import ar.edu.utn.frsf.tpae.a2013.g04.presentation.util.ServiceLocator;
import ar.edu.utn.frsf.tpae.a2013.g04.service.SolicitudGastoService;

@ManagedBean(name="administrarSolicitudesBean")
@SessionScoped
public class AdministrarSolicitudesBean {
	@ManagedProperty(value="#{usuarioBean.user}")
	private Supervisor supervisor;
    private Map<Integer, SolicitudGasto> solmap;
    private SolicitudGasto solSelected;
    private String description;
    
	public List<SolicitudGasto> getListarPendietnes(){
		try{
			solmap = new HashMap<Integer, SolicitudGasto>();
			List<SolicitudGasto> sollist = ServiceLocator.getSolicitudGastoService()
					                                     .listarSolicitudPendientes();
			for(SolicitudGasto q:sollist){
				solmap.put(q.getId(),q);
			}
			return new ArrayList<SolicitudGasto>(solmap.values());
		}
		catch(RuntimeException e){
			// I ate the exception gnum gnum!!
		}
		return null;
	}
	
	public String aceptar(Integer solId){
		solSelected = solmap.get(solId);
		return "navigateToAceptarSolicitud";
	}
	public String rechazar(Integer solId){
		solSelected = solmap.get(solId);
		return "navigateToRechazarSolicitud";
	}
	
	public String aceptarSolicitud(){
		try{
			
		SolicitudGastoService serv = ServiceLocator.getSolicitudGastoService();
		solSelected.setSupervisor(supervisor);
		serv.aprobarSolicitud(solSelected, description);
		}
		catch(RuntimeException e){
			
		}
		return "navigateToSupervisor";
	}
    
	public String rechazarSolicitud(){
		try{

			SolicitudGastoService serv = ServiceLocator.getSolicitudGastoService();
			solSelected.setSupervisor(supervisor);
			serv.rechazarSolicitud(solSelected, description);
			}
			catch(RuntimeException e){
				
			}
			return "navigateToSupervisor";
	}
	public Supervisor getSupervisor() {
		return supervisor;
	}

	public void setSupervisor(Supervisor supervisor) {
		this.supervisor = supervisor;
	}

	public SolicitudGasto getSolSelected() {
		return solSelected;
	}

	public void setSolSelected(SolicitudGasto solSelected) {
		this.solSelected = solSelected;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
